package com.yt.ds.chapter3;

/**
 * @Author: lzf
 * @Date: 2020/7/25 14:35
 * @Description: 无序链表
 */
public class SequentialSearchST<Key, Value> {
    private Node first;

    private class Node {
        Key key;
        Value val;
        Node next;

        public Node(Key key, Value val, Node next) {
            this.key = key;
            this.val = val;
            this.next = next;
        }
    }

    /**
     * 描述：取目标键对应结点的值
     *
     * @param key 目标键
     * @return "Value"
     * @date: 2020/7/25 14:43
     */
    public Value get(Key key) {
        for (Node node = first; node != null; node = node.next) {
            if (key.equals(node.key)) {
                return node.val;
            }
        }
        return null;
    }

    /**
     * 描述：设置，存在则更新，不存在往头部添加
     *
     * @param key 键
     * @param val 值
     * @return "void"
     * @date: 2020/7/25 14:43
     */
    public void put(Key key, Value val) {
        for (Node node = first; node != null; node = node.next) {
            if (key.equals(node.key)) {
                node.val = val;
                return;
            }
        }
        // 头插
        first = new Node(key, val, first);
    }
}
